package aiku.numericalcontroloa.project.system.service.impl;

import aiku.numericalcontroloa.common.annotation.ExceptionMessage;
import aiku.numericalcontroloa.common.utils.SysMenuTreeUtil;
import aiku.numericalcontroloa.project.system.mapper.SysMenuMapper;
import aiku.numericalcontroloa.project.system.pojo.entity.SysMenu;
import aiku.numericalcontroloa.project.system.pojo.query.SysMenuParams;
import aiku.numericalcontroloa.project.system.pojo.query.SysRoleParams;
import aiku.numericalcontroloa.project.system.pojo.vo.SysMenuVo;
import aiku.numericalcontroloa.project.system.service.SysMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    @ExceptionMessage("删除菜单数据失败")
    public int deleteByPrimaryKey(SysMenuParams params) {
        //如果有子级菜单或功能则不能删除
        List<SysMenu> menus = sysMenuMapper.selectChildByPrimaryKey(params.getId());
        if (menus.size() == 0) {
            return sysMenuMapper.deleteByPrimaryKey(params.getId());
        }
        return 0;
    }


    @Override
    @ExceptionMessage("新增菜单数据失败")
    public int insert(SysMenu record) {
        if (record.getType() == 1) {
            record.setPath("");
            record.setComponent("");
        }
//        record.setCreateUser(RequestContext.getCurrentUserModel().getUsername());
        record.setCreateUser("admin");
        record.setCreateTime(new Date());
        return sysMenuMapper.insert(record);
    }

    @Override
    @ExceptionMessage("获取菜单数据失败")
    public SysMenu selectByPrimaryKey(Integer id) {
        return sysMenuMapper.selectByPrimaryKey(id);
    }

    @Override
    @ExceptionMessage("修改菜单数据失败")
    public int updateByPrimaryKey(SysMenu record) {
//        record.setModifyUser(RequestContext.getCurrentUserModel().getUsername());
//        record.setModifyTime(new Date());
        return sysMenuMapper.updateByPrimaryKey(record);

    }

    @Override
    @ExceptionMessage("获取菜单数据失败")
    public List<SysMenuVo> menuTree(String uid) {
        List<SysMenu> menuList = sysMenuMapper.listByUserId(uid);
        List<SysMenu> menus = SysMenuTreeUtil.listToTree(menuList);
        List<SysMenuVo> convert = SysMenuTreeUtil.convert(menus);
        return convert;
//        return SysMenuTreeUtil.convert(menus);
    }

    @Override
    @ExceptionMessage("获取菜单数据失败")
    public List<SysMenu> allMenuTree() {
        List<SysMenu> menuList = sysMenuMapper.allMenuTree();
        return SysMenuTreeUtil.listToTree(menuList);
    }

    @Override
    @ExceptionMessage("获取菜单数据失败")
    public List<SysMenu> listByRoleId(SysRoleParams query) {
        List<SysMenu> menuList = sysMenuMapper.listByRoleId(query.getId());

        return menuList;
    }

    @Override
    @ExceptionMessage("获取菜单数据失败")
    public List<SysMenu> onlyAllMenuTree() {
        List<SysMenu> menuList = sysMenuMapper.onlyAllMenuTree();
        return SysMenuTreeUtil.listToTree(menuList);
    }

    @Override
    public List<SysMenu> selectChildByPrimaryKey(SysMenuParams query) {
        return sysMenuMapper.selectChildByPrimaryKey(query.getId());
    }


}
